﻿using MyabpDDD.Demo.Application.LinemanagementtableLy.Dto;
using MyCompany.MyProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyabpDDD.Demo.Application.LinemanagementtableLy
{
    public class LinemanagementtableLyService : ApplicationService, ILinemanagementtableLyService
    {
        private readonly IRepository<Linemanagementtable> _repository;
        public LinemanagementtableLyService(IRepository<Linemanagementtable> students)
        {
            this._repository = students;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Delete(int id)
        {
            var list = _repository.Where(r => r.Id == id).FirstOrDefault();

            await _repository.DeleteAsync(list);
        }
        /// <summary>
        /// 查看详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public dynamic Get(int id)
        {
            var list = _repository.Where(r => r.Id == id).FirstOrDefault();
            return list;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Linemanagementtable> Inser(LinemanagementtableLyDto dto)
        {
            Linemanagementtable www = ObjectMapper.Map<LinemanagementtableLyDto, Linemanagementtable>(dto);
            return await _repository.InsertAsync(www);
        }

        public dynamic QueryStudentAll(int pageIndex, int pageSize, string xianname, string qidian, string zhundian, string shifou, string phone, string danwei)
        {
            var q1 = _repository.AsQueryable();
            if (!string.IsNullOrEmpty(xianname))
            {
                q1=q1.Where(x=>x.Nametheline==xianname);
            }
            if (!string.IsNullOrEmpty(qidian))
            {
                q1=q1.Where(x=>x.Startingpoint==qidian);
            }
            if (!string.IsNullOrEmpty(zhundian))
            {
                q1 = q1.Where(x => x.Theend == zhundian);
            }
            if (!string.IsNullOrEmpty(shifou))
            {
                q1=q1.Where(x=>x.Outsourcing==shifou);
            }
            if (!string.IsNullOrEmpty(phone))
            {
                q1=q1.Where(x=>x.Mobilephone==phone);
            }
            if (!string.IsNullOrEmpty(danwei))
            {
                q1=q1.Where(x=>x.Ownerunit==danwei);
            }
            var list = new
            {
                Zongtiao = q1.Count(),
                data = q1.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()
            };
            return list;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public bool SelectDel(string ids)
        {
            string[] int_ids = ids.Split(',');
            var q = from id in int_ids
                    select Convert.ToInt32(id);
            var q_list = q.ToList();
            var query = _repository.AsQueryable();
            query = query.Where(d => q_list.Contains
              (d.Id));
            foreach (var model in query)
            {
                _repository.DeleteAsync(model);
            }
            return true;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public async Task<bool> update(LinemanagementtableLyDto dto)
        {
            Linemanagementtable www = ObjectMapper.Map<LinemanagementtableLyDto, Linemanagementtable>(dto);
            var db = _repository.GetDbSet<Linemanagementtable>();
            db.Update(www);
            return true;
        }
    }
}
